var ifm = require('../models/ifm.js');
var cfm = require('../models/cfm.js');
var ifmIndex = require('../models/ifmIndex.js');
var cfmIndex = require('../models/cfmIndex.js');
var ObjectID = require("mongodb").ObjectID;

exports.on = function(req,res) {
    var filter = {};
    filter["_id"] = ObjectID(req.params.id);
    console.log(filter);

    cfmIndex.get(filter,function(err,doc){
        console.log("+++++++");
        console.log(doc);
        res.render('user-involve', {
            title: 'Project Involved',
            user : req.session.user,
            cfm  : doc[0],
            success : "",
            error : ""
        });
    });
};

exports.intro = function(req,res){
    console.log('user-uninvolve');
    checkExistence(req,res,function(state,doc){
        var ifm = doc;
        cfmIndex.get({_id : req.params.id},function(err,doc){
            var cfm = doc[0];
            res.render('user-uninvolve', {
                title: 'Project uninvolve',
                user : req.session.user,
                state : state,
                ifm : ifm,
                cfm : cfm,
                success : "",
                error : ""
            });
        });
    })
}

checkExistence = function(req,res,callback){
    var filter = {};
    filter["user"] = req.params.user;

    ifmIndex.get(filter,function(err,doc){
        if(doc[0] === undefined){
            ifmIndex.create(filter,function(err,doc){
                return res.redirect(req.url);
            });
        }else{
            if(doc[0][ObjectID(req.params.id)] != undefined){
                callback(true,doc[0][ObjectID(req.params.id)]);
            }else{
                callback(false,null);
            }
        }
    });
}

exports.create = function(req,res){
    var filter = {};
    filter["user"] = req.params.user;

    ifmIndex.get(filter,function(err,doc){
        if(doc[0] === undefined){
            ifmIndex.create(filter,function(err,doc){
                return res.redirect(req.url);
            });
        }else{
            if(doc[0][ObjectID(req.params.id)] != undefined){
                //如果已存在就直接跳转
                return res.redirect('/'+req.params.user+'/involved/'+doc[0][ObjectID(req.params.id)]["cfm_id"]);
            }else{
                //否则进行创建
                var filter2 = {};
                filter2["_id"] =ObjectID(req.params.id);

                 cfmIndex.get(filter2,function(err,doc){
                     var cfm_index = doc[0];
                     var filter = [{user: req.params.user},{}];
                     filter[1][ObjectID(req.params.id)] = {
                         cfm_id : req.params.id
                         ,name : cfm_index["name"]
                         ,description: cfm_index["description"]
                     };

                     ifmIndex.update(filter,function(err){
                         console.log(err);
                         console.log(doc);
                         res.redirect('/'+req.params.user+'/involved/'+req.params.id);
                     });
                 });
            }
        }
    });
}